package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.ScriptResultNoticeRequest;
import cc.rengu.igas.channel.cups.facade.response.ScriptResultNoticeResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.igas.share.facade.bean.cups.F60_3_5_TxnSendMethodEnum;
import cc.rengu.igas.share.facade.bean.cups.F70_NetMngInfCodeEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;

/**
 * 脚本结果通知
 *
 * @author Administrator
 */
public class CupsScriptResultNoticeService extends BasePayService<ScriptResultNoticeRequest, ScriptResultNoticeResponse> {

    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean,
                                             ScriptResultNoticeRequest scriptResultNoticeRequest,
                                             ScriptResultNoticeResponse scriptResultNoticeResponse) throws Exception {
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        CupsField cupsField = cupsFieldBean.getCupsField();
        BeanUtil.beanCopy(scriptResultNoticeRequest, cupsField);
        cupsField.setMsgType("0620");
        cupsField.setProcCode(scriptResultNoticeRequest.getProcCode());
        if ("01".equals(scriptResultNoticeRequest.getTermType())) {
            cupsField.setConCode("02");
        } else {
            if ("0100".equals(scriptResultNoticeRequest.getOrigMsgType())) {
                cupsField.setConCode("06");
            } else {
                cupsField.setConCode("00");
            }
        }

        cupsField.setPayAcctNo(scriptResultNoticeRequest.getPayAcctNo());
        cupsField.setTransDateTime(scriptResultNoticeRequest.getTransDateTime());
        cupsField.setCupsTraceNo(scriptResultNoticeRequest.getCupsTraceNo());
        cupsField.setLocalDate(scriptResultNoticeRequest.getOrigTransDateTime().substring(0, 4));
        cupsField.setLocalTime(scriptResultNoticeRequest.getOrigTransDateTime().substring(4));
        cupsField.setManageNetCode(F70_NetMngInfCodeEnum._951.getCode());
        if (scriptResultNoticeRequest.getOrigRespCode().length() > 2) {
            int respCodeLength = scriptResultNoticeRequest.getOrigRespCode().length();
            cupsField.setRespCode(scriptResultNoticeRequest.getOrigRespCode().substring(respCodeLength - 2));
        } else {
            cupsField.setRespCode(scriptResultNoticeRequest.getOrigRespCode());
        }

        if ("00000000".equals(scriptResultNoticeRequest.getOrigRespCode())) {
            cupsField.setRespCode("00");
        } else {
            cupsField.setRespCode(scriptResultNoticeRequest.getOrigRespCode());
        }

        cupsField.setMchntType(scriptResultNoticeRequest.getMchntType());
        cupsField.setMchntNo(scriptResultNoticeRequest.getMchntNo());
        cupsField.setTermNo(scriptResultNoticeRequest.getTermNo());
        cupsField.setMchntShortName(scriptResultNoticeRequest.getMchntShortName());
        cupsField.setAcqInstCode(scriptResultNoticeRequest.getAcqInstCode());
        cupsField.setFwdInstCode(scriptResultNoticeRequest.getFwdInstCode());
        cupsField.setReferenceNo(scriptResultNoticeRequest.getReferenceNo());

        cupsFieldBean.createF90()
                .setF90_1_OrigF0(scriptResultNoticeRequest.getOrigMsgType())
                .setF90_2_OrigF11(scriptResultNoticeRequest.getOrigCupsSsn())
                .setF90_3_OrigF7(scriptResultNoticeRequest.getOrigTransDateTime());
        cupsFieldBean.createF90()
                .setF90_4_OrigF32(scriptResultNoticeRequest.getAcqInstCode())
                .setF90_5_OrigF33(scriptResultNoticeRequest.getFwdInstCode());

        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        //  特殊计费类型
        cupsFieldBean.createF60()
                .setF60_2_2(scriptResultNoticeRequest.getTermRead())
                .setF60_2_3(scriptResultNoticeRequest.getIcConCode())
                .setF60_2_5(scriptResultNoticeRequest.getTermType())
                .setF60_3_5(F60_3_5_TxnSendMethodEnum._0.getCode());
        cupsFieldBean.build();

        String tmpAmt = AmountUtil.changeY2F(scriptResultNoticeRequest.getOrderAmt());
        cupsField.setOrderAmt(String.format("%012d", Integer.valueOf(tmpAmt)));

        //  调用 CUPS    不生成MAC和验MAC   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, scriptResultNoticeRequest.getHeader().getTxnNum(), null);

        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, scriptResultNoticeResponse);
        scriptResultNoticeResponse.setRespCode(cupsFieldRsp.getRespCode());

        if (CupsParamConstant.SUCCESS_CODE.equals(scriptResultNoticeResponse.getRespCode())) {
            scriptResultNoticeResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            scriptResultNoticeResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(scriptResultNoticeResponse);
        return chnlBizResponse;
    }

}
